<?php
class SearchController extends DefaultController{
	public function _default(){
		$key = request('key');
		
		$catalog_id = request('catalog_id');
		
		if (!$key){
			$this->error('没有指定搜索关键字');
		}
		
		$whereArr[] = "(`title` like '%$key%' or `memo` like '%$key%')";
		$whereArr[] = "`status`='normal'";
		
		if ($catalog_id){
			$childNote = $this->_childNote($catalog_id);
			$ids = array_keys($childNote);
			if ($ids){
				$whereArr[] = "`catalog_id` in (".implode(',', $ids).")";
			}else{
				$this->error('指定栏目不存在');
			}
		}
		
		$where = implode(' and ', $whereArr);
		
		$page = $this->page(ArchiveModel::getCount($where));
		
		$archives = ArchiveModel::getList($where, $this->start, $this->perpage);
		
		$archives = $this->_formatArchive($archives);
		
		$this->assign('archives', $archives);
		$this->assign('page', $page);
		$this->display();
	}
}